package anbook.dao.controller;

import anbook.dao.impl.MemberDAOImpl;
import anbook.dto.Friend;
import anbook.dto.Friends_request;
import anbook.dto.Member;
import anbook.util.Matcher;

public class MemberController extends MemberDAOImpl{
	
	public boolean account(Member member){
		Matcher matcher = new Matcher();
		
		String email = member.getMb_email();
		if (email.isEmpty()) {
			return false;
		}
		email = email.trim();
		if (!matcher.isEmail(email)){
			return false;
		}
		member.setMb_email(email);
		
		String password = member.getMb_password();
		String nicname = member.getMb_nicname();
		String sex = member.getMb_sex();
		
		if (email.length() < 5 || email.length() > 50)
			return false;
		else if (password.length() < 8 || password.length() > 20)
			return false;
		else if (!(sex.equals("m") || sex.equals("w")))
			return false;
		else if (nicname.length() < 2 || nicname.length() > 50)
			return false;
		
		return insertMember(member);
	}
	
	public boolean login(Member member){
		Member obj = getMember(member.getMb_email());
		if (obj == null){
			return false;
		}
		
		if (obj.getMb_password().equals(member.getMb_password()))
			return true;
		else
			return false;
	}
	
	public void friendRequest(Friends_request request){
		Friend f_ck = new Friend();
		f_ck.setMe_email(request.getRequest_email());
		f_ck.setFriend_email(request.getFriend_email());
		
		if (checkFriendRequest(request) && checkFriend(f_ck))
			insertFriendRequest(request);
	}
	
	public void acceptRequest(Friends_request request){
		if (!checkFriendRequest(request)){
			Friend friend_me = new Friend();
			Friend friend_you = new Friend();
			
			friend_me.setMe_email(request.getRequest_email());
			friend_me.setFriend_email(request.getFriend_email());
			friend_you.setMe_email(request.getFriend_email());
			friend_you.setFriend_email(request.getRequest_email());
			
			Friends_request request_rv = new Friends_request();
			request_rv.setFriend_email(request.getRequest_email());
			request_rv.setRequest_email(request.getFriend_email());
			deleteFriendRequest(request);
			deleteFriendRequest(request_rv);
			
			insertFriend(friend_me);
			insertFriend(friend_you);
		}
	}
}
